/**
 * 验证工具类
 */

// 验证邮箱
export const isEmail = (email: string): boolean => {
  const reg = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
  return reg.test(email);
};

// 验证手机号
export const isPhone = (phone: string): boolean => {
  const reg = /^1[3-9]\d{9}$/;
  return reg.test(phone);
};

// 验证密码强度（至少包含数字和字母，长度8-20位）
export const isStrongPassword = (password: string): boolean => {
  const reg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,20}$/;
  return reg.test(password);
};

// 验证用户名（字母开头，允许字母数字下划线，长度4-20位）
export const isValidUsername = (username: string): boolean => {
  const reg = /^[a-zA-Z][a-zA-Z0-9_]{3,19}$/;
  return reg.test(username);
};

// 验证URL
export const isUrl = (url: string): boolean => {
  const reg = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/;
  return reg.test(url);
};

// 验证身份证号
export const isIdCard = (idCard: string): boolean => {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  return reg.test(idCard);
};

// 验证是否为空
export const isEmpty = (value: any): boolean => {
  return value === "" || value === null || value === undefined;
};

// 验证是否为数字
export const isNumber = (value: any): boolean => {
  return !isNaN(Number(value));
};

// 验证是否为整数
export const isInteger = (value: any): boolean => {
  return Number.isInteger(Number(value));
};

// 验证是否为正数
export const isPositive = (value: any): boolean => {
  return Number(value) > 0;
};

// 验证是否为中文
export const isChinese = (value: string): boolean => {
  const reg = /^[\u4e00-\u9fa5]+$/;
  return reg.test(value);
};
